package cn.online.util;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class MyFileUpload extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	public String  fileUp(File file,String fileName){
		
		DateFormat format = new SimpleDateFormat("yyMMddHHmmss");   
        String formatDate = format.format(new Date());
        int random = new Random().nextInt(10000);  
        int position = fileName.lastIndexOf(".");   
        String extension = fileName.substring(position);  
        fileName = formatDate+random+extension;
        String targetDirectory = ServletActionContext.getRequest().getRealPath("/upload");
        File target = new File(targetDirectory, fileName);
           try {
        	   FileUtils.copyFile(file, target); 
		} catch (Exception e) {
			e.printStackTrace();
		}
        return fileName;
	}

		
	
}
